using UnityEngine;
using System.Collections;

public class CharacterMovement_Cemetery : DrawableGameObject 
{
	public Texture2D[] standTextures, walkTextures, jumpTextures, climbTextures;
	public Texture2D[] standTextures_Invisible, walkTextures_Invisible, jumpTextures_Invisible;
	public Texture2D[] standTextures_Temp, walkTextures_Temp, jumpTextures_Temp;
	private float horizontal;
	private float speed = 0.1f;
	float timer;
	
	bool jumping = false;
	bool canJump = false;
	bool isInvisible = false;
	
	Vector3 startScale;
	
	GameObject[] BGs;
	
	public override void Start () 
	{
		base.SetAnimations(standTextures, walkTextures, jumpTextures, null);
		base.Start();
		
		startScale = transform.localScale;
	}
	
	void Update () 
	{
		Move ();
		RunAnimations();
	}
	
	void Move()
	{
		horizontal = Input.GetAxis("Horizontal") * speed;
		transform.Translate(horizontal, 0, 0);
		
		if(Input.GetKeyDown(KeyCode.Space))
		{
			if(canJump)
			{
				gameObject.rigidbody.AddForce(0,400,0);
			}
		}
		
		if(Input.GetKeyDown("k"))
		{
			for (int i = 0; i < standTextures.Length; i++) 
			{
				standTextures[i] = standTextures_Invisible[i];
			}
			
			for (int i = 0; i < walkTextures.Length; i++) 
			{
				walkTextures[i] = walkTextures_Invisible[i];
			}
			
			for (int i = 0; i < jumpTextures.Length; i++) 
			{
				jumpTextures[i] = jumpTextures_Invisible[i];
			}
			
			isInvisible = true;
		}
		
		if(isInvisible)
		{
			timer += Time.deltaTime;
		}
		
		if(timer > 6)
		{
			timer = 0;
			isInvisible = false;
			
			for (int i = 0; i < standTextures.Length; i++) 
			{
				standTextures[i] = standTextures_Temp[i];
			}
			
			for (int i = 0; i < walkTextures.Length; i++) 
			{
				walkTextures[i] = walkTextures_Temp[i];
			}
			
			for (int i = 0; i < jumpTextures.Length; i++) 
			{
				jumpTextures[i] = jumpTextures_Temp[i];
			}
		}
		
		BGs = GameObject.FindGameObjectsWithTag("Background");
	}
	
	void RunAnimations()
	{
		if(!canJump)
		{
			base.gameAnimation.CalculateFps(1);
			base.RunAnimation ("Jump", "OneShot");
		}
		
		if(horizontal == 0)
		{
			if(canJump)
			{
				base.gameAnimation.CalculateFps(15);
				base.RunAnimation("Stand", "Loop");
			}
		}
		
		if(horizontal > 0)
		{
			if(canJump)
			{
				base.gameAnimation.CalculateFps(15);
				base.RunAnimation("Walk", "Loop");
			}
			
			transform.localScale = new Vector3(startScale.x, startScale.y, startScale.z);
			
			foreach(GameObject bg in BGs)
			{
				bg.GetComponent<Parallax>().SetParallax("Normal");
			}
		}
		
		if(horizontal < 0)
		{
			if(canJump)
			{
				base.gameAnimation.CalculateFps(15);
				base.RunAnimation("Walk", "Loop");
			}
			
			transform.localScale = new Vector3(-startScale.x, startScale.y, startScale.z);
			
			foreach(GameObject bg in BGs)
			{
				bg.GetComponent<Parallax>().SetParallax("Reverse");
			}
		}
	}
	
	public void OnCollisionStay(Collision other)
	{
		if(other.gameObject.tag == "Ground")
		{
			canJump = true;
		}
	}
	
	public void OnCollisionExit(Collision other)
	{
		if(other.gameObject.tag == "Ground")
		{
			canJump = false;
		}
	}
}
